package start.spring.basic.flowable.service.form.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;

import start.spring.basic.flowable.dao.WfExternalFormMapper;
import start.spring.basic.flowable.entity.WfExternalForm;
import start.spring.basic.flowable.service.BaseServiceImpl;
import start.spring.basic.flowable.service.form.IWfExternalFormService;
import start.spring.basic.page.PageQuery;
import start.spring.basic.page.PageResult;
import start.spring.basic.util.common.StringUtil;

/**
 * 表单管理Service业务层处理
 * 
 * @author xuyy
 * @date 2023-08-24
 */
@Service
public class WfExternalFormServiceImpl extends BaseServiceImpl implements IWfExternalFormService {
    
    @Autowired
    private WfExternalFormMapper wfExternalFormMapper;

    /**
     * 查询表单管理列表
     * 
     * @param wfExternalForm 表单管理
     * @return 表单管理
     */
    @Override
    public PageResult<WfExternalForm> selectWfExternalFormPageList(PageQuery pageQuery,WfExternalForm wfExternalForm) {
        IPage<WfExternalForm> page = super.getPage(pageQuery);
        QueryWrapper<WfExternalForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(!StringUtil.isEmpty(wfExternalForm.getFormId()), WfExternalForm::getFormId, wfExternalForm.getFormId());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfExternalForm.getFormName()), WfExternalForm::getFormName, wfExternalForm.getFormName());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfExternalForm.getFormUrl()), WfExternalForm::getFormUrl, wfExternalForm.getFormUrl());
        queryWrapper.lambda().like(!StringUtil.isEmpty(wfExternalForm.getRemark()), WfExternalForm::getRemark, wfExternalForm.getRemark());
        page = this.wfExternalFormMapper.selectPage(page, queryWrapper);
        return super.getPageResult(page);
    }
    
    /**
     * 查询全量表单管理
     * 
     * @param wfExternalForm 表单管理
     * @return 表单管理
     */
    @Override
    public List<WfExternalForm> selectWfExternalFormList(WfExternalForm wfExternalForm) {
        QueryWrapper<WfExternalForm> queryWrapper = new QueryWrapper<>();
        if(wfExternalForm!=null) {
        	queryWrapper.lambda().eq(!StringUtil.isEmpty(wfExternalForm.getFormId()), WfExternalForm::getFormId, wfExternalForm.getFormId());
            queryWrapper.lambda().like(!StringUtil.isEmpty(wfExternalForm.getFormName()), WfExternalForm::getFormName, wfExternalForm.getFormName());
            queryWrapper.lambda().like(!StringUtil.isEmpty(wfExternalForm.getFormUrl()), WfExternalForm::getFormUrl, wfExternalForm.getFormUrl());
            queryWrapper.lambda().like(!StringUtil.isEmpty(wfExternalForm.getRemark()), WfExternalForm::getRemark, wfExternalForm.getRemark());
        }
        return this.wfExternalFormMapper.selectList(queryWrapper);
    }

    /**
     * 新增表单管理
     * 
     * @param wfExternalForm 表单管理
     * @return 结果
     */
    @Override
    public int insertWfExternalForm(WfExternalForm wfExternalForm) {
    	wfExternalForm.setCreateBy(String.valueOf(super.getSysUser().getUserName()));
    	wfExternalForm.setCreateTime(new Date());
        return this.wfExternalFormMapper.insert(wfExternalForm);
    }

    /**
     * 修改表单管理
     * 
     * @param wfExternalForm 表单管理
     * @return 结果
     */
    @Override
    public int updateWfExternalForm(WfExternalForm wfExternalForm) {
    	wfExternalForm.setUpdateBy(String.valueOf(super.getSysUser().getUserName()));
    	wfExternalForm.setUpdateTime(new Date());
    	UpdateWrapper<WfExternalForm> updateWrapper = new UpdateWrapper<>();
    	updateWrapper.lambda().eq(WfExternalForm::getFormId, wfExternalForm.getFormId());
    	//非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	updateWrapper.lambda().eq(WfExternalForm::getCreateBy, super.getSysUser().getUserName());
        }
    	return this.wfExternalFormMapper.update(wfExternalForm, updateWrapper);
    }

    /**
     * 批量删除表单管理
     * 
     * @param formIds 需要删除的表单管理主键
     * @return 结果
     */
    @Override
    public int deleteWfExternalFormByFormIds(List<String> formIds) {
    	QueryWrapper<WfExternalForm> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().in(WfExternalForm::getFormId, formIds);
        //非管理员登录
        if(!super.getSysUser().isAdmin()) {
        	queryWrapper.lambda().eq(WfExternalForm::getCreateBy, super.getSysUser().getUserName());
        }
        return this.wfExternalFormMapper.delete(queryWrapper);
    }
	
}
